<?php
namespace Crm\Common;

class CommonHelper
{
    
    static public function safe($text, $type = "html", $tagsMethod = true, $attrMethod = true, $xssAuto = 1, $tags = array( ), $attr = array( ), $tagsBlack = array( ), $attrBlack = array( )){
        $text_tags = "";
        $font_tags = "<i><b><u><s><em><strong><font><big><small><sup><sub><bdo><h1><h2><h3><h4><h5><h6>";
        $base_tags = $font_tags."<p><br><hr><a><img><map><area><pre><code><q><blockquote><acronym><cite><ins><del><center><strike>";
        $form_tags = $base_tags."<form><input><textarea><button><select><optgroup><option><label><fieldset><legend>";
        $html_tags = $base_tags."<ul><ol><li><dl><dd><dt><table><caption><td><th><tr><thead><tbody><tfoot><col><colgroup><div><span><object><embed>";
        $all_tags = $form_tags.$html_tags."<!DOCTYPE><html><head><title><body><base><basefont><script><noscript><applet><object><param><style><frame><frameset><noframes><iframe>";
        $text = strip_tags( $text, ${ $type."_tags" } );
        if($type != "all"){
            while ( preg_match( "/(<[^><]+) (onclick|onload|unload|onmouseover|onmouseup|onmouseout|onmousedown|onkeydown|onkeypress|onkeyup|onblur|onchange|onfocus|action|background|codebase|dynsrc|lowsrc)([^><]*)/i", $text, $mat ) )
            {
                $text = str_ireplace( $mat[0], $mat[1].$mat[3], $text );
            }
            while ( preg_match( "/(<[^><]+)(window\\.|javascript:|js:|about:|file:|document\\.|vbs:|cookie)([^><]*)/i", $text, $mat ) )
            {
                $text = str_ireplace( $mat[0], $mat[1].$mat[3], $text );
            }
        }
        return $text;
    }
    
    static public function paramArrayToString($array) {
        $str = '';
        foreach ($array as $key=>$value) {
            if ($value != null) {
                $str .= '&'.$key.'='.$value;
            }
        }
        
        return $str;
    }

    static public function orgSelectWeek()
    {
        
        $week = array(
            'w1' => '星期一',
            'w2' => '星期二',
            'w3' => '星期三',
            'w4' => '星期四',
            'w5' => '星期五',
            'w6' => '星期六',
            'w7' => '星期日',
            'w8' => '星期一',
            'w9' => '星期二',
            'w10' => '星期三',
            'w11' => '星期四',
            'w12' => '星期五',
            'w13' => '星期六',
            'w14' => '星期日',
        
        );
        
        $select_week = array();
        for ($i = 1; $i<=14; $i++){
            $_time= strtotime("+".$i." day");
            $_days=floor($_time/(60*60*24));
            $_num=($_days+3)%14+1;
            $select_week['w'.$_num] = $week['w'.$_num]. '&nbsp;&nbsp;&nbsp;&nbsp;' .date("Y-m-d", $_time);
        }
        return $select_week;
        
    }

    //双引号  替换 单引号
    public function doubleQuotationMarksTosingleQuotationMarks($string){
        return str_replace("\"", '\'', $string);
    }
    
    static public function browserExport($type, $filename)
    {
        if ($type == "Excel5") {
            header('Content-Type: application/vnd.ms-excel'); // excel03
        } else {
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // excel07
        }
        $ua = $_SERVER["HTTP_USER_AGENT"];
        $encoded_filename = urlencode($filename);
        $encoded_filename = str_replace("+", "%20", $encoded_filename);
        if (preg_match("/MSIE/", $ua)) {
            header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
        } else if (preg_match("/Firefox/", $ua)) {
            header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $filename . '"');
        }
        
        //header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
    }

    public function httpRquest($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        
        if (! empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        
        $res = curl_exec($curl);
        if (curl_errno($curl)) {
            return 'Errno' . curl_error($curl);
        }
        curl_close($curl);
        
        return $res;
    }

    function turnMsgToObj($post_str)
    {
        if (! empty($post_str)) {
            $result = simplexml_load_string($post_str, 'SimpleXMLElement', LIBXML_NOCDATA);
        } else {
            $result = NULL;
        }
        
        return $result;
    }

    public function getClientIp()
    {
        $ip = $_SERVER['REMOTE_ADDR'];
        if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
            foreach ($matches[0] as $xip) {
                if (! preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
                    $ip = $xip;
                    break;
                }
            }
        }
        return $ip;
    }
}